# 876. 链表的中间结点

// 给定一个头结点为 head 的非空单链表,返回链表的中间结点。
// 如果有两个中间结点,则返回第二个中间结点。
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
  let length = 0;
  let headCopy = head;
  const map = new Map();
  while (headCopy) {
    length++;
    map.set(length, headCopy);
    headCopy = headCopy.next;
  }
  const index = Math.ceil((length + 1) / 2);
  return map.get(index);
};

console.log(
  middleNode({
    val: 1,
    next: {
      val: 2,
      next: { val: 3, next: { val: 4, next: { val: 5, next: null } } },
    },
  })
); // [3,4,5]
console.log(
  middleNode({
    val: 1,
    next: {
      val: 2,
      next: {
        val: 3,
        next: { val: 4, next: { val: 5, next: { val: 6, next: null } } },
      },
    },
  })
); // [3,4,5]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Last Updated: 6/27/2023, 7:40:45 PM